//
//  HCFindContentMoreView.m
//  HappyChildren
//
//  Created by WillyZhao on 2016/12/29.
//  Copyright © 2016年 WillyZhao. All rights reserved.
//

#import "HCFindContentMoreView.h"

#import "HCFindContentBottomView.h"
#import "HCFindCollectionViewCell.h"

#import "HCFindModel.h"
#import "HCAllUsedModel.h"
#import "HCEveryDayModel.h"

@interface HCFindContentMoreView () <UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout> {
    id data;
    HCAllUsedModel *alluse;
    HCEveryDayModel *every;
}

@end

@implementation HCFindContentMoreView

-(instancetype)initWithFrame:(CGRect)frame andStyle:(HCFindContentMoreViewStyle)style {
    if (self = [super initWithFrame:frame]) {
        [self initializeAllSubViewFrame:frame withStyle:style];
    }
    return self;
}

-(void)initializeAllSubViewFrame:(CGRect)frame withStyle:(HCFindContentMoreViewStyle)style {
    self.style = style;
//    self.backgroundColor = [UIColor blackColor];
    //顶部提示
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, YBKAdaptiveH(58))];
    imageView.userInteractionEnabled = YES;
    [self addSubview:imageView];
    
    //底部查看更多
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, frame.size.height - YBKAdaptiveH(40), frame.size.width, YBKAdaptiveH(40))];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self addSubview:bottomView];
    
    CGSize size_ll = [HCUserTools sizeWithText:@"查看更多" withFont:YBKSystemFont(YBKAdaptiveFont(11))];
    
    HCFindContentBottomView *vv = [[HCFindContentBottomView alloc] initWithFrame:CGRectMake((frame.size.width - size_ll.width - 13)/2.0, (bottomView.frame.size.height - size_ll.height)/2.0, 20, 20)];
    vv.backgroundColor = [UIColor whiteColor];
    [bottomView addSubview:vv];
    
    
    //中部内容
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.minimumLineSpacing = 0;
    flow.minimumInteritemSpacing = 0;
    self.moreCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, imageView.frame.origin.y+imageView.frame.size.height, frame.size.width,bottomView.frame.origin.y-imageView.frame.size.height) collectionViewLayout:flow];
    self.moreCollectionView.delegate = self;
    self.moreCollectionView.dataSource = self;
    self.moreCollectionView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.moreCollectionView];
    
    [self.moreCollectionView registerNib:[UINib nibWithNibName:@"HCFindCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"cellfind"];
    
    switch (style) {
        case HCFindContentMoreViewHotRecom:
            //热推
        {
            imageView.image = [UIImage imageNamed:@"happy_find_contentHotRecom"];
        }
            break;
        case HCFindContentMoreViewDay:
            //每日特价
            imageView.image = [UIImage imageNamed:@"happy_find_contentDayPrice"];
            break;
        case HCFindContentMoreViewAll:
            //大家都在用
            imageView.image = [UIImage imageNamed:@"happy_find_contentAllUsed"];
            break;
            break;
        default:
            break;
    }
}

#pragma mark UICollectionView代理
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 2;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HCFindCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellfind" forIndexPath:indexPath];
    if (data) {

        if ([data isKindOfClass:[HCFindModel class]]) {
            HCFindData *data1 = ((HCFindModel *)data).f_index[indexPath.row];
            cell.model = data1;
        }
        
        if ([data isKindOfClass:[HCEveryDayModel class]]) {
            HCEveryDayData *data1 = ((HCEveryDayModel *)data).all_used[indexPath.row];
            cell.model = data1;
        }
        
        if ([data isKindOfClass:[HCAllUsedModel class]]) {
            HCAllUsedData *data1 = ((HCAllUsedModel *)data).sp_offer[indexPath.row];
            cell.model = data1;
        }
    }
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(YBKWinFrame.size.width/2.0, YBKAdaptiveH(210));
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if ([self.delegate respondsToSelector:@selector(HCFindContentMoreViewClicked:view:)]&&self.delegate) {
        [self.delegate HCFindContentMoreViewClicked:indexPath view:self];
    }
}

-(void)setModel:(id)model {
    
    data = model;
    [self.moreCollectionView reloadData];

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
